package com.hx.er.action.admin.user;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;

import com.hx.er.action.base.ErAdminAction;
import com.hx.er.bean.base.UserBean;
import com.hx.er.common.entity.LoginInfoEntity;
import com.hx.er.common.logic.SessionUtil;
import com.hx.er.common.logic.UserLoginInfoUtil;
import com.hx.er.common.system.Constants;
import com.hx.er.common.system.ErProperties;
import com.hx.er.common.system.ErSessions;
import com.hx.er.common.util.MD5Util;
import com.hx.er.service.admin.IAdminUserService;
import com.opensymphony.xwork2.ActionContext;

/**
 * 用户登录、登出Action
 * 
 * @author HX
 * @version 1.0
 * @since 1.0
 */
public class AdminLoginAction extends ErAdminAction {
	// 序列化ID
	private static final long serialVersionUID = 4926512300151605899L;

	private UserBean userBeanPage;

	@Autowired
	private IAdminUserService adminUserService;

	public String init() {
		return SUCCESS;
	}

	public String login() {
		// 检查用户登录次数
		int currentCount = UserLoginInfoUtil.getUserLoginCount(this.userBeanPage.getUserName());
		// 获取配置量
		String limitCountStr = ErProperties.getProperty(Constants.PROPERTY_LIMIT_LOGIN_FAILURE_COUNT);
		// 极限次数
		int limitCount = Integer.parseInt(limitCountStr);
		// 判断登录失败次数是否超过限制
		if (currentCount >= limitCount) {
			this.addFieldError("msg.count", String.format("登录失败已经超过%s次。", limitCount));
			return LOGIN;
		}

		// 密码转换MD5
		this.userBeanPage.setPassword(MD5Util.encodeMD5(this.userBeanPage.getPassword()));
		// 设定用户类型
		this.userBeanPage.setUserTypeId(Constants.USER_TYPE_ADMIN);

		// 检索用户信息，并存入session，以后使用可以直接从session中取，即super.userInfoLogin
		super.userInfoLogin = this.adminUserService.getUserAlive(this.userBeanPage);

		if (super.userInfoLogin == null) {
			// 输出错误信息
			this.addFieldError("msg.input", "用户名或密码错误，请重新登录。");
			
			// 失败次数记录
			int countNew = UserLoginInfoUtil.userLoginCount(this.userBeanPage.getUserName());
			// 提示登录失败
			this.addFieldError("msg.count", String.format("已登录失败%s次，最多%s次。", countNew, limitCount));
		
			return LOGIN;
		} else {
			// session中增加用户信息
			SessionUtil.setAttributes(ActionContext.getContext(), Constants.SESSION_USER_INFO, super.userInfoLogin);
			// 将用户登录信息计入内存
			ErSessions.login(super.request.getSession().getId());

			// 记录登录成功数据
			LoginInfoEntity loginInfo = new LoginInfoEntity();
			loginInfo.setUserId(super.userInfoLogin.getUpdateUserId());
			loginInfo.setRestId(0);
			loginInfo.setUserTypeId(Constants.USER_TYPE_ADMIN);
			loginInfo.setFailureCount(UserLoginInfoUtil.getUserLoginCount(super.userInfoLogin.getUserName()));
			loginInfo.setIpAddress(super.ipLong);
			loginInfo.setLoginDate(new Date(System.currentTimeMillis()));
			this.adminUserService.insertLoginInfoWithoutId(loginInfo);

			return SUCCESS;
		}
	}

	public String logout() {
		// 清除登录信息中的数据
		ErSessions.logout(super.request.getSession().getId());

		// 清空session中用户信息
		super.userInfoLogin = null;
		SessionUtil.removeAttribute(ActionContext.getContext(), Constants.SESSION_USER_INFO);

		// 页面跳转
		return SUCCESS;
	}

	public UserBean getUserBeanPage() {
		return userBeanPage;
	}

	public void setUserBeanPage(UserBean userBeanPage) {
		this.userBeanPage = userBeanPage;
	}
}
